package com.yun.newBlog.blog.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yun.newBlog.blog.dao.blog.BlogMapper;
import com.yun.newBlog.blog.domain.entity.Blog;
import com.yun.newBlog.blog.domain.entity.Comment;
import com.yun.newBlog.blog.service.IBlogService;
import com.yun.newBlog.blog.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.web.bind.annotation.*;
import tk.mybatis.spring.annotation.MapperScan;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@MapperScan(basePackages = {"com.yun.newBlog.blog.dao.blog"})
@RestController
@CrossOrigin
@RequestMapping("/blog")
@Slf4j
@Api(tags = "博客管理")
public class BlogController {

    @Autowired
    private IBlogService blogService;

    @Autowired
    private BlogMapper blogMapper;

    @ApiOperation("保存评论")
    @PostMapping("/comment/save")
    public CommonResponseVo addComment(HttpServletRequest request,@RequestBody Comment comment) {
        blogService.addBlogComment(comment);

        CommonResponseVo response = new CommonResponseVo();
        response.setErrNo(0);
        response.setMsg("新增成功");
        return response;
    }

    @ApiOperation("获取标签列表")
    @RequestMapping("/tag/list")
    public List<TagVO> listAllTag(){
        return blogService.getTagList();
    }

    @ApiOperation("博客列表分页查询")
    @RequestMapping("/query/page")
    public PageInfo<BlogPageQueryOutVO>  queryForPage(@RequestParam String sort, @RequestParam String tag, @RequestParam Integer current) {
        BlogQueryVo queryVo = new BlogQueryVo();
        queryVo.setSort(sort);
        queryVo.setTag(tag);
        queryVo.setCurrent(current);
        Page<BlogPageQueryOutVO> page = blogService.queryForPage(queryVo);
        return new PageInfo<>(page.getResult(),(int)page.getTotal());
    }

    @ApiOperation("查询博客列表")
    @RequestMapping("/query/list")
    public List<BlogPageQueryOutVO> queryForAll(@RequestParam String sort,@RequestParam String tag) {
        BlogQueryVo queryVo = new BlogQueryVo();
        queryVo.setSort(sort);
        queryVo.setTag(tag);
        return blogService.queryBlogList(queryVo);
    }

    @ApiOperation("博客列表模糊搜索")
    @RequestMapping("/query/like")
    public List<BlogPageQueryOutVO> queryListByLike(@RequestParam String like,@RequestParam String sort,@RequestParam String tag) {
        BlogQueryVo blogQueryVo = new BlogQueryVo();
        blogQueryVo.setTag(tag);
        blogQueryVo.setSort(sort);
        blogQueryVo.setLike(like);
        return blogService.queryBlogListByLike(blogQueryVo);
    }

    /**
     * 单篇文章查询
     * @param id
     * @return
     */
    @ApiOperation("博客单条查询")
    @RequestMapping("/query/{id}")
    public BlogVO queryForOne(@PathVariable("id") Integer id) {
        return blogService.queryBlog(id);
    }

    @ApiOperation("保存博客")
    @PostMapping("/save")
    public CommonResponseVo saveBlog(@RequestBody Blog blog){
        blogService.save(blog);
        CommonResponseVo response = new CommonResponseVo();
        response.setErrNo(0);
        response.setMsg("新增成功");
        return response;
    }

}